﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

using EdgeStatz.ESSystems.Objects;

namespace EdgeStatz
{
    public partial class ParametersDisplay : Form
    {
        ES_R_Report report;
        ReportParameterInfoCollection param_info;

        public ReportParameterInfoCollection ParameterInfo { get { return param_info; } set { param_info = value; } }

        public ES_R_ReportParameter[] Parameters { get { return report.ReportParameters.ToArray(); } }

        public ParametersDisplay(ES_R_Report report)
        {
            InitializeComponent();
            this.report = report;
            this.report.Refresh_ParamTables();
            int rows = this.report.ReportParameters.Count;

            this.tableLayoutPanel1.RowCount = rows;
            this.tableLayoutPanel1.RowStyles.Clear();
            for (int i = 0; i < rows + 1;i++ )
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 25F));

            for (int i = 0; i < rows; i++)
            {
                Label lbl = new Label();
                lbl.Text = this.report.ReportParameters[i].Prompt;
                lbl.Size = new Size(100, 25);
                this.tableLayoutPanel1.Controls.Add(lbl, 0, i);

                CheckBox ck_null = new CheckBox();
                ck_null.Name = "ck_null_" + i;
                ck_null.Checked = false;
                ck_null.Text = "Null";
                ck_null.Enabled = false;
                ck_null.CheckedChanged += new EventHandler(ck_null_CheckedChanged);
                this.tableLayoutPanel1.Controls.Add(ck_null, 2, i);

                if (this.report.ReportParameters[i].ParameterType == ES_R_ParameterTypes.DataSetReference)
                {
                    ComboBox cb_param = new ComboBox();
                    cb_param.Name = "cb_param_" + i;
                    cb_param.DropDownStyle = ComboBoxStyle.DropDownList;
                    cb_param.Size = new Size(400, 25);
                    cb_param.SelectedValueChanged += new EventHandler(cb_param_SelectedValueChanged);
                    if (this.report.ReportParameters[i].Table == null)
                        cb_param.Enabled = false;
                    else
                    {
                        DataTable table = this.report.ReportParameters[i].Table;
                        int col_display = table.Columns[this.report.ReportParameters[i].Dataset_LabelField].Ordinal;
                        int col_value = table.Columns[this.report.ReportParameters[i].Dataset_ValueField].Ordinal;
                        foreach(DataRow row in table.Rows)
                        {
                            cb_param.Items.Add(new DisplayValueObject(row.ItemArray[col_display].ToString(), row.ItemArray[col_value].ToString()));
                        }
                        cb_param.Text = "<Select One>";
                    }
                    cb_param.DisplayMember = "Display";
                    cb_param.ValueMember = "Value";
                    this.tableLayoutPanel1.Controls.Add(cb_param, 1, i);                    
                }                
                if (this.report.ReportParameters[i].Nullable)
                    ck_null.Enabled = true;
            }

            Button b_OK = new Button();
            b_OK.Dock = DockStyle.Right;
            b_OK.Text = "OK";
            b_OK.Click += new EventHandler(b_OK_Click);

            Button b_Cancel = new Button();
            b_Cancel.Dock = DockStyle.Right;
            b_Cancel.Text = "Cancel";
            b_Cancel.Click += new EventHandler(b_Cancel_Click);

            Panel p_Buttons = new Panel();
            p_Buttons.Controls.Add(b_OK); 
            p_Buttons.Controls.Add(b_Cancel);             
            p_Buttons.Dock = DockStyle.Bottom;
            p_Buttons.Size = new Size(400, 25);
            this.tableLayoutPanel1.Controls.Add(p_Buttons, 1, rows);           
        }

        void ck_null_CheckedChanged(object sender, EventArgs e)
        {
            string name = ((CheckBox)sender).Name;
            int index = Convert.ToInt32(name.Substring(name.Length - 1));

            ((ComboBox)this.tableLayoutPanel1.Controls["cb_param_" + index]).Enabled = !((CheckBox)sender).Checked;
            ((ComboBox)this.tableLayoutPanel1.Controls["cb_param_" + index]).SelectedIndex = -1;
            this.report.ReportParameters[index].Value = null;
            this.report.ReportParameters[index].Label = null;

            RefreshParameters(index);
        }

        void cb_param_SelectedValueChanged(object sender, EventArgs e)
        {
            if (((ComboBox)sender).SelectedItem == null)
                return;
            string name = ((ComboBox)sender).Name;
            int index = Convert.ToInt32(name.Substring(name.Length - 1));
            this.report.ReportParameters[index].Value = ((DisplayValueObject)((ComboBox)sender).SelectedItem).Value;
            this.report.ReportParameters[index].Label = ((ComboBox)sender).Text;

            RefreshParameters(index);
        }

        void b_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        void b_OK_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.report.ReportParameters.Count; i++)
            {
                if (this.report.ReportParameters[i].ParameterType == ES_R_ParameterTypes.DataSetReference)
                {
                    //if (report.ReportParameters[i].Value == null &&
                    //    ((ComboBox)this.tableLayoutPanel1.Controls["cb_param_" + i]).SelectedValue != null)
                    //{
                    //    this.report.ReportParameters[i].Value = ((ComboBox)this.tableLayoutPanel1.Controls["cb_param_" + i]).SelectedValue;
                    //    this.report.ReportParameters[i].Label = ((ComboBox)this.tableLayoutPanel1.Controls["cb_param_" + i]).Text;
                    //}

                    if (!this.report.ReportParameters[i].Nullable &&
                        this.report.ReportParameters[i].Value == null)
                    //((ComboBox)this.tableLayoutPanel1.Controls["cb_param_" + i]).SelectedValue == null)
                    { MessageBox.Show("Parameter [" + report.ReportParameters[i].Prompt + "] cannot be null.", "Incorrect Parameter", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        void RefreshParameters(int index)
        {
            if (param_info != null && index > 0)
            {
                ReportParameterInfo info = null;
                for (int i = 0; i < param_info.Count; i++)
                    if (param_info[i] != null && this.report.ReportParameters[index].Name.ToLower().Equals(param_info[i].Name.ToLower()))
                        info = param_info[i];
                if (info != null && info.Dependents.Count > 0)
                    foreach (ReportParameterInfo rpi in info.Dependents)
                        for (int j = 0; j < this.report.ReportParameters.Count; j++)
                            if (rpi.Name.ToLower().Equals(this.report.ReportParameters[j].Name.ToLower()))
                            {
                                this.report.ReportParameters[j].Value = null;
                                this.report.ReportParameters[j].Label = null;
                            }
            }
            this.report.Refresh_ParamTables();
            for (int i = 0; i < this.report.ReportParameters.Count; i++)
            {
                if (this.report.ReportParameters[i].ParameterType == ES_R_ParameterTypes.DataSetReference
                    && report.ReportParameters[i].Value == null && !(
                        ((CheckBox)this.tableLayoutPanel1.Controls["ck_null_" + i]).Enabled && ((CheckBox)this.tableLayoutPanel1.Controls["ck_null_" + i]).Checked
                    ))
                {
                    ComboBox cb_param = (ComboBox)this.tableLayoutPanel1.Controls["cb_param_" + i];
                    if (cb_param == null)
                        continue;
                    if(this.report.ReportParameters[i].Table == null)
                        cb_param.Enabled = false;
                    else
                    {
                        cb_param.Enabled = true;
                        DataTable table = this.report.ReportParameters[i].Table;
                        int col_display = table.Columns[this.report.ReportParameters[i].Dataset_LabelField].Ordinal;
                        int col_value = table.Columns[this.report.ReportParameters[i].Dataset_ValueField].Ordinal;
                        cb_param.Items.Clear();
                        foreach (DataRow row in table.Rows)
                        {
                            cb_param.Items.Add(new DisplayValueObject(row.ItemArray[col_display].ToString(), row.ItemArray[col_value].ToString()));
                        }
                        cb_param.SelectedValue = null;
                    }
                    this.tableLayoutPanel1.Controls.Add(cb_param, 1, i);
                }
            }
        }
    }
}
